﻿

'*******************************************************************
' Program: Data Center Visio Rack Tool
' Author: Albert E Edlund
' Date:
'
' Purpose:
'
'
'
'*******************************************************************


Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging


Imports System
Imports System.ComponentModel
Imports System.Collections.Generic

Imports Microsoft.SqlServer.Types

Imports Microsoft.Office.Interop.Visio.VisDefaultColors
Imports System.Runtime.InteropServices

Public Class dc_Link
    Implements INotifyPropertyChanged


#Region "Events"

    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs) Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    Private Sub OnPropertyChanged(ByVal info As String)
        RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
    End Sub

#End Region

#Region " Constructor "

    Public Sub New()
        initProperties()
    End Sub

    Public Sub New(ByVal strUserId As String)
        initProperties()
        Me.m_linklastedit = strUserId
    End Sub

#End Region

#Region " Enumerations "

    ' 
    ' the intent is to support a variety of physical/local and logical/wan 
    ' link types
    '
    Public Enum enumLinkTypes
        linkUnknown = -1

        linkCopper = 31         ' ethernet
        linkFiber = 32          ' fiber optic
        linkCoax = 33
        linkSerial = 34
        linkUSB = 35
        linkVendor = 36         ' the conn type

        linkPrivateLine = 41    ' wan
        linkFrame = 42          ' wan
        linkInternet = 43       ' wan
        linkIntranet = 44       ' wan
        linkWAN = 40            ' generic local
        linkLAN = 30            ' generic wan
    End Enum

    '
    ' support the ability to color code the links
    '
    Public Enum enumLinkColors
        linkCopper = visBlue
        linkFiber = visRed
        linkUSB = visGreen
        linkSerial = visYellow
        linkOther = visBlack
    End Enum

    '
    ' link state is physical/logical 
    '
    Public Enum enumLinkState
        logical
        physical
        unknown
    End Enum

    Public Enum linkRoute
        lr_intraRack
        lr_interRack
        lr_Wan
        lr_Unterminated
        lr_Undefined
    End Enum


#End Region

#Region " Properties and Fields "


    Private m_linkkey As Int32
    Const constLinkKey As Int32 = -1
    Property LinkKey() As Int32
        Get
            Return Me.m_linkkey
        End Get
        Set(ByVal value As Int32)
            Me.m_linkkey = value
            Me.OnPropertyChanged("linkKey")
        End Set
    End Property

    Private m_linkcablelabel As String
    Const constLinkCableLabel As String = "none"
    Property linkCableLabel() As String
        Get
            Return Me.m_linkcablelabel
        End Get
        Set(ByVal value As String)
            Me.m_linkcablelabel = value
            Me.OnPropertyChanged("linkCableLabel")
        End Set
    End Property

    Private m_linkportakey As Int32
    Const constLinkPortAKey As Int32 = -1
    Property linkPortAKey() As Int32
        Get
            Return Me.m_linkportakey
        End Get
        Set(ByVal value As Int32)
            Me.m_linkportakey = value
            Me.OnPropertyChanged("linkPortAKey")
        End Set
    End Property

    Private m_linkportbkey As Int32
    Const constLinkPortBKey As Int32 = -1
    Property linkPortBKey() As Int32
        Get
            Return Me.m_linkportbkey
        End Get
        Set(ByVal value As Int32)
            Me.m_linkportbkey = value
            Me.OnPropertyChanged("linkPortBKey")
        End Set
    End Property

    Private m_linkType As enumLinkTypes
    Const constlinkType As enumLinkTypes = enumLinkTypes.linkCopper
    Property linkType() As enumLinkTypes
        Get
            Return Me.m_linkType
        End Get
        Set(ByVal value As enumLinkTypes)
            If Not TypeDescriptor.GetConverter(GetType(enumLinkTypes)).IsValid(value) Then
                ' Throw New ArgumentException()
                value = enumLinkTypes.linkUnknown
            End If
            Me.m_linkType = value
            Me.OnPropertyChanged("linkType")
        End Set
    End Property

    ' place holder
    Private m_linkMediaLength As Double
    Const constLinkMediaLength As Double = 0
    Property linkMediaLength() As Double
        Get
            Return Me.m_linkMediaLength
        End Get
        Set(ByVal value As Double)
            Me.m_linkMediaLength = value
            Me.OnPropertyChanged("linkMediaLength")
        End Set
    End Property

    ' text version of the geospatial data used in sql server 2008 geography
    Private m_linktxtSpatial As String
    Const constlinkTxtSpatial As String = "LINESTRING EMPTY"
    Public Property linktxtSpatial() As String
        Get
            Return Me.m_linktxtSpatial
        End Get
        Set(ByVal value As String)
            Me.m_linktxtSpatial = value
            Me.OnPropertyChanged("linktxtSpatial")
        End Set
    End Property

    ' binary placeholder for geodata
    Private m_linkSpatial As Microsoft.SqlServer.Types.SqlGeography
    ' Const constlinkSpatial As Microsoft.SqlServer.Types.SqlGeography = Nothing
    Public Property linkSpatial() As Microsoft.SqlServer.Types.SqlGeography
        Get
            Return Me.m_linkSpatial
        End Get
        Set(ByVal value As Microsoft.SqlServer.Types.SqlGeography)
            Me.m_linkSpatial = value
            Me.OnPropertyChanged("linkSpatial")
        End Set
    End Property

    Private m_linkspeed As Int32
    Const constLinkSpeed As Int32 = 0
    Property linkSpeed() As Int32
        Get
            Return Me.m_linkspeed
        End Get
        Set(ByVal value As Int32)
            Me.m_linkspeed = value
            Me.OnPropertyChanged("linkSpeed")
        End Set
    End Property

    ' path latency in milliseconds
    Private m_linklatency As Double
    Const constLinkLatency As Double = 0
    Property linkLatency() As Double
        Get
            Return Me.m_linklatency
        End Get
        Set(ByVal value As Double)
            Me.m_linklatency = value
            Me.OnPropertyChanged("linkLatency")
        End Set
    End Property

    ' our physical/flag to help in diagramming wans
    ' we intentionally are passing on wan types that may be either a physical/logical links (frame ?)
    Private m_linkplstate As enumLinkState
    Const constLinkPLState As enumLinkState = enumLinkState.physical
    Property linkPLState() As enumLinkState
        Get
            Return Me.m_linkplstate
        End Get
        Set(ByVal value As enumLinkState)
            If Not TypeDescriptor.GetConverter(GetType(enumLinkState)).IsValid(value) Then
                ' Throw New ArgumentException()
                value = enumLinkState.unknown
            End If
            Me.m_linkplstate = value
            Me.OnPropertyChanged("linkPLState")
        End Set
    End Property


    Private m_linkComments As String
    Const constLinkComments As String = "na"
    Property linkComments() As String
        Get
            Return Me.m_linkComments
        End Get
        Set(ByVal value As String)
            Me.m_linkComments = value
            Me.OnPropertyChanged("linkComments")
        End Set
    End Property

    Private m_linkLastUpdate As Date
    Const constLinkLastUpdate As Date = #1/1/2001#
    Property linkLastUpdate() As Date
        Get
            Return Me.m_linkLastUpdate
        End Get
        Set(ByVal value As Date)
            Me.m_linkLastUpdate = value
            Me.OnPropertyChanged("linkLastUpdate")
        End Set
    End Property

    Private m_linklastedit As String
    Const constLinkLastEdit As String = ""
    Property linkLastEdit() As String
        Get
            Return Me.m_linklastedit
        End Get
        Set(ByVal value As String)
            Me.m_linklastedit = value
            Me.OnPropertyChanged("linkLastEdit")
        End Set
    End Property



#End Region

#Region " Methods "

    '
    '
    '
    Public Sub initProperties()

        Try

            LinkKey = constLinkKey
            linkCableLabel = constLinkCableLabel
            linkPortAKey = constLinkPortAKey
            linkPortBKey = constLinkPortBKey
            linkType = constlinkType
            linkMediaLength = constLinkMediaLength
            linktxtSpatial = constlinkTxtSpatial
            linkSpatial = Nothing
            linkSpeed = constLinkSpeed
            linkLatency = constLinkLatency
            linkPLState = constLinkPLState
            linkComments = constLinkComments
            linkLastUpdate = Now()
            linkLastEdit = constLinkLastEdit


        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' pass in an enumerated link type, return a string 
    '
    Public Function getLinkType(ByVal intLinkType As Integer) _
            As String

        Dim strReturn As String = ""
        Try

            ' if an invalid link type is passed in then make it an unknown so we do not fail
            If Not TypeDescriptor.GetConverter(GetType(enumLinkTypes)).IsValid(intLinkType) Then
                intLinkType = enumLinkTypes.linkUnknown
            End If

            Dim myName As enumLinkTypes = intLinkType
            strReturn = myName.ToString

        Catch com As COMException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        Return strReturn

    End Function

    '
    ' pass in a link type and get back a linkstate as a string
    '
    Public Function getLinkState(ByVal intLinkType As enumLinkTypes) _
            As String

        Dim intReturn As enumLinkState = enumLinkState.unknown
        Dim strReturn As String = ""

        Try

            Select Case intLinkType
                Case enumLinkTypes.linkCoax, enumLinkTypes.linkCopper, enumLinkTypes.linkFiber, enumLinkTypes.linkSerial, _
                    enumLinkTypes.linkUSB, enumLinkTypes.linkVendor
                    intReturn = enumLinkState.physical
                Case enumLinkTypes.linkFrame, enumLinkTypes.linkInternet, enumLinkTypes.linkIntranet, enumLinkTypes.linkPrivateLine, _
                    enumLinkTypes.linkWAN, enumLinkTypes.linkLAN
                    intReturn = enumLinkState.logical
            End Select

            Dim myName As enumLinkState = intReturn
            strReturn = myName.ToString


        Catch com As COMException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        Return strReturn

    End Function



#End Region


End Class

